
$(document).ready(function(){
    // render
    loadData();
    // event
    bindEventEdit();
});

function loadData() {
    var _maSP = ISS.SESSION.getSession('session_maSP');

    var product = {};
    product.maSP = _maSP;

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/editProductCommon',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(product),
        success: function (data) {
            var responseContent = data.responseObject;
            loadDataComplete(responseContent);
        }
    });

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/getAllProductListInProduct',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapAllProductListToViewInProduct(responseContent);
        }
    });

}

function loadDataComplete(product){
    ISS.DOM.setVal('#ProductCommon_code', product.maSP);
    ISS.DOM.setVal('#ProductCommon_name', product.tenSP);
    ISS.DOM.setVal('#ProductCommon_listProduct_hidden', product.loaiDanhMucSanPham.maLoaiDMSP);
    ISS.DOM.setVal('#ProductCommon_information', product.thongTin);
    ISS.DOM.setVal('#ProductCommon_sexual_hidden', product.doiTuong);
    ISS.DOM.setVal('#ProductCommon_sexual', product.doiTuong);

}

function bindEventEdit(){
    ISS.EVENT.bindEvent('#ProductCommon_Edit1', 'click', function () {
        sendInfoEdit();
    });

    ISS.EVENT.bindEvent('#buttonEditProductDetail_Cancel', 'click', function () {
        ISS.DOM.addClass('#boxTrackConfirm', 'hideBox');
    });

    ISS.EVENT.bindEvent('#buttonEditProductDetail_Agree', 'click', function () {
        ISS.DOM.removeClass('#boxTrackConfirm', 'hideBox');
        /*ISS.DOM.removeClass('#boxDetailProduct', 'hideBox');
        ISS.DOM.addClass('#buttonProductCommon', 'hideBox');*/
        toDetailPage();
    });

    ISS.EVENT.bindEvent('#ProductCommon_Exit', 'click', function () {
        ISS.DOM.removeClass('#boxTrackExitConfirm', 'hideBox');
    });

    ISS.EVENT.bindEvent('#buttonExitProductDetail_Agree', 'click', function () {
        var _url = ISS.SESSION.getSession('contextPath') + '/ad-product-common.jsp';
        ISS.WINDOW.location(_url);
    });

    ISS.EVENT.bindEvent('#buttonExitProductDetail_Cancel', 'click', function () {
        ISS.DOM.addClass('#boxTrackExitConfirm', 'hideBox');
    });
}

function sendInfoEdit() {
    var product = {};
    product.maSP = ISS.DOM.getVal('#ProductCommon_code');
    product.tenSP = ISS.DOM.getVal('#ProductCommon_name');
    product.doiTuong = ISS.DOM.getVal('#ProductCommon_sexual');
    product.thongTin = ISS.DOM.getVal('#ProductCommon_information');

    var category = {};
    category.maLoaiDMSP = ISS.DOM.getVal('#ProductCommon_listProduct');
    product.loaiDanhMucSanPham = category;

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/registerProductEdit',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(product),
        success: function (data) {
            var responseContent = data.responseObject;
            sendInfoEditComplete(responseContent);
        }
    });
}

function sendInfoEditComplete(product) {
    if(product) {
//        ISS.WINDOW.location('/jsp/admin/ad-product-size-common.jsp');
//        alert('Cập nhật thành công');
        ISS.DOM.removeClass('#boxTrackConfirm', 'hideBox');
    }
    else{
        alert('Cập nhật không thành công');
    }
}

function toDetailPage() {
    var _maSP = ISS.DOM.getVal('#ProductCommon_code');
    var _url = '/service/admin/toProductDetailPage/' + _maSP;
    ISS.WINDOW.location(_url);
}

function mapAllProductListToViewInProduct(productList) {
    var productList_code = ISS.DOM.getVal('#ProductCommon_listProduct_hidden');
    var n = '';
    var html = '';
    for(var i=0; i<productList.length; i++) {
        if (productList[i].maLoaiDMSP == productList_code){
            n = 'selected="selected"';
        }
        else{
            n = '';
        }
        html += '<option value="' + productList[i].maLoaiDMSP + '" ' + n + '  >' + productList[i].tenLoaiDMSP + '</option>';
    }
    ISS.DOM.html('#ProductCommon_listProduct', html);

}


/*
function mapAllProductListToViewInProduct(productList) {
    var html = '';
    for(var i=0; i<productList.length; i++) {
        html += '<option value="' + productList[i].maLoaiDMSP +'" >' + productList[i].tenLoaiDMSP + '</option>';
    }
    ISS.DOM.html('#ProductCommon_listProduct', html);

}*/
